package com.joolun.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;

import com.joolun.common.utils.StringUtils;
import com.joolun.mall.entity.Work;

import com.joolun.mall.entity.WorkClick;
import com.joolun.mall.service.WorkClickService;
import com.joolun.mall.service.WorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/api/ma/wrok")
@Api(value = "work", tags = "实习")
public class WorkApiController extends BaseController {
    private final WorkService baseService;
    private final WorkClickService workClickService;

    @ApiOperation(value = "通过id查询实习")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") String id){
        return AjaxResult.success(baseService.getById(id));
    }

    @ApiOperation(value = "分页查询实习")
    @GetMapping("/page")
    public AjaxResult getByPage(Page page, Work work ){
        IPage iPage = baseService.page1(page, work);
        return AjaxResult.success(iPage);
    }

    /**
     * 新增实习投递记录
     * @param entity 实习投递记录
     * @return R
     */
    @ApiOperation(value = "新增实习投递记录-相同记录不会新增")
    @PostMapping("/workClick")
    public AjaxResult save(@RequestBody WorkClick entity){
        List<WorkClick> list = workClickService.list(new QueryWrapper<WorkClick>().eq("work_id", entity.getWorkId()).eq("wx_user_id", entity.getWxUserId()));
        if(list.isEmpty()){
            entity.setCreateTime(LocalDateTime.now());
            return AjaxResult.success(workClickService.save(entity));
        }else{
            return AjaxResult.success();
        }
    }

}
